//
//  XMBadgeView.m
//  XMWeibo
//
//  Created by chi on 14-8-20.
//  Copyright (c) 2014年 xiaoming. All rights reserved.
//

#import "XMBadgeView.h"

#define kEdge 5.0

@implementation XMBadgeView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        self.adjustsImageWhenHighlighted = NO;
        
        self.userInteractionEnabled = NO;
//        self.backgroundColor = [UIColor greenColor];
        
        //设置标题
        [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        [self.titleLabel setFont:[UIFont systemFontOfSize:12.0]];
    }
    return self;
}

- (void)setBadgeValue:(NSString *)text
{
    self.hidden = NO;
    
    if ([text length] > 1) {
        UIImage *image = [UIImage stretchableImageWithName:@"main_badge"];
        
        [self setBackgroundImage:image forState:UIControlStateNormal];
        
        CGSize titleSize = [text sizeWithFont:self.titleLabel.font];
        
        self.bounds = CGRectMake(0.0, 0.0, titleSize.width + kEdge * 2, image.size.height);
        
    }
    else if ([text length] > 0) {
        
        UIImage *image = [UIImage adaptIOS7ImageWithName:@"main_badge"];
        
        [self setBackgroundImage:image forState:UIControlStateNormal];
        
        self.bounds = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
    }
    else {
        self.hidden = YES;
    }
    
    [self setTitle:text forState:UIControlStateNormal];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    
}

@end
